package com.example.springboot01;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamSkill {

    public static class Cls {
        private String name;
        private Double score;

        public Cls(String name, Double score) {
            this.name = name;
            this.score = score;
        }
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setScore(Double score) {
            this.score = score;
        }

        public Double getScore() {
            return score;
        }
    }

    public static class Cls1 {
        private String name;
        private Double score;

        public Cls1(String name, Double score) {
            this.name = name;
            this.score = score;
        }
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setScore(Double score) {
            this.score = score;
        }

        public Double getScore() {
            return score;
        }
    }

    public static void main(String[] args) {

        final List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);

        // 分组
        final Map<Integer, List<Integer>> collect = list.stream().collect(Collectors.groupingBy(x -> x));
        System.out.println(collect);

        // 排序
        list.stream().sorted(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        // 转换为 map
        final List<Cls> clsList = Arrays.asList(new Cls("name1", 96D), new Cls("name2", 65D));
        final Map<String, Double> map = clsList.stream().collect(Collectors.toMap(x -> x.name, x -> x.getScore()));

        // 转换为其他对象列表
        final List<Cls1> cls1s = clsList.stream().map(x -> new Cls1(x.name, x.score)).collect(Collectors.toList());

        // 求和 均值
        final double sum = clsList.stream().mapToDouble(Cls::getScore).sum();

        final double avg = clsList.stream().mapToDouble(Cls::getScore).average().getAsDouble();
    }
}
